package com.bank.basic.controller;

import com.bank.basic.service.LoanService;
import com.bank.basic.service.impl.LoanServiceImpl;
import com.bank.common.commonUtil.AuthContextHolderUtil;
import com.bank.model.entity.Deposit;
import com.bank.model.entity.Loan;
import com.bank.model.entity.User;
import com.bank.model.vo.loan.LoanManagementVo;
import com.bank.model.vo.loan.LoanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Cejay1314
 * @version 1.0
 * @create 2023/2/1 13:49
 */
@RestController
@RequestMapping(value = "/basic/loan")
public class LoanController {
    @Autowired
    LoanService loanService;

    @RequestMapping("")
    public String demo(){
        User user = new User();
        return user.toString();
//        return "d";
    }
    @GetMapping("/query/list")
    public List<LoanVo> queryListVo(HttpServletRequest httpServletRequest){
        Long userId = AuthContextHolderUtil.getUserId(httpServletRequest);
        return loanService.queryListVo(userId);
    }
    @GetMapping("/query/management/list")
    List<LoanManagementVo> queryLoanManagementVo(){
        return loanService.queryLoanManagementVo();
    }
    @GetMapping("/account")
    public List<Loan> queryAccount(@RequestParam String accountId){
        return loanService.queryAccount(accountId);
    }
    @GetMapping("/user")
    public List<Loan> queryUser(@RequestParam Long userId){
        return loanService.queryUser(userId);
    }
    @PostMapping("/add")
    public void addLoan(@RequestBody LoanVo loanVo, HttpServletRequest httpServletRequest) throws IOException {
        Long borrowId = AuthContextHolderUtil.getUserId(httpServletRequest);
        loanVo.setBorrowId(borrowId);
        loanService.addLoan(loanVo,null);
    }
    @PostMapping("/add/mortgage")
    public void addLoanMortgage(LoanVo loanVo, MultipartFile file, HttpServletRequest httpServletRequest) throws IOException {
        Long borrowId = AuthContextHolderUtil.getUserId(httpServletRequest);
        loanVo.setBorrowId(borrowId);
        loanService.addLoan(loanVo,file);
    }

    @GetMapping("/update/status")
    void updateLoanState(@RequestParam Long loanId,@RequestParam Boolean ifPass){
        loanService.updateLoanState(loanId,ifPass);
    }
    @GetMapping("/del")
    void delLoan(@RequestParam Long loanId){
        loanService.delLoan(loanId);
    }

//    @PostMapping("/add")
//    void download(Long loanId, HttpServletResponse httpServletResponse){
//
//    }
}
